package com.example.medioplayer.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.medioplayer.R;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.yan.pullrefreshlayout.PullRefreshLayout;
import com.yan.pullrefreshlayout.PullRefreshView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Test4 extends Fragment {
    private  RecyclerView recyclerView;
    private PullRefreshLayout refreshLayout;
    public Test4() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View view=inflater.inflate(R.layout.fragment_test4, container, false);
        refreshLayout = (PullRefreshLayout) view.findViewById(R.id.pullrefresh);
        recyclerView= (RecyclerView) view.findViewById(R.id.recycleview);
        initRefreshLayout();
        return view;
    }

    private void init() {

    }

    private void initRefreshLayout() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        recyclerView.setAdapter(new MyAdapter());
        refreshLayout.setLoadMoreEnable(true);
//        refreshLayout.setDuringAdjustValue(0.4f);// 动画执行时间调节，越大动画执行越快
        // 刷新或加载完成后回复动画执行时间，为0是，根据setDuringAdjustValue（0.4f）方法实现
//        refreshLayout.setRefreshBackTime(300);
//        refreshLayout.setPullViewHeight(100);// 设置头部和底部的高度
        refreshLayout.setDragDampingRatio(0.4f);// 阻尼系数
        refreshLayout.setPullFlowHeight(400);// 拖拽最大范围
//        refreshLayout.setRefreshEnable(false);
//        refreshLayout.setUseAsTwinkLayout(true);
        refreshLayout.setHeaderView(new PullRefreshView(getActivity().getBaseContext()) {
            TextView tv;

            @Override
            protected void initView() {
                tv = (TextView) findViewById(R.id.title);
            }

            @Override
            protected int contentView() {
                return R.layout.refresh_view;
            }

            @Override
            public void onPullChange(float percent) {
            //    Log.e(TAG, "onPullChange: refresh " + percent);
            }

            @Override
            public void onPullReset() {
                tv.setText("下拉");
            }

            @Override
            public void onPullHoldTrigger() {
                tv.setText("释放刷新");
            }

            @Override
            public void onPullHoldUnTrigger() {
                tv.setText("下拉");
            }

            @Override
            public void onPullHolding() {
                tv.setText("正在刷新");
            }

            @Override
            public void onPullFinish() {
                tv.setText("刷新完成");
            }
        });
        refreshLayout.setFooterView(new PullRefreshView(getActivity().getBaseContext()) {
            TextView tv;
            ProgressBar pb;

            @Override
            protected void initView() {
                tv = (TextView) findViewById(R.id.title);
                pb= (ProgressBar) findViewById(R.id.pb);
            }

            @Override
            protected int contentView() {
                return R.layout.load_more_view;
            }

            @Override
            public void onPullChange(float percent) {
               // Log.e(TAG, "onPullChange: loadMore " + percent);
            }

            @Override
            public void onPullReset() {
                tv.setText("上拉");
                pb.setVisibility(View.GONE);
            }

            @Override
            public void onPullHoldTrigger() {
                tv.setText("释放加载");
                pb.setVisibility(View.GONE);
            }

            @Override
            public void onPullHoldUnTrigger() {
                tv.setText("上拉");
                pb.setVisibility(View.GONE);
            }

            @Override
            public void onPullHolding() {

                tv.setText("正在加载");
                pb.setVisibility(View.VISIBLE);

            }

            @Override
            public void onPullFinish() {
                tv.setText("加载完成");
                pb.setVisibility(View.GONE);
            }
        });
        refreshLayout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.postDelayed(new Runnable() {
                    int a=0;
                    @Override
                    public void run() {
                        refreshLayout.refreshComplete();
                    }
                }, 2000);
            }

            @Override
            public void onLoading() {
                int i=0;
                refreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.loadMoreComplete();
                    }
                }, 2000);
            }
        });
    }


    //内部类
    class MyAdapter extends RecyclerView.Adapter{

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test4_recycle,parent,false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 0;
        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            public MyViewHolder(View view){
                super(view);
            }
        }
    }

}
